package utils

import (
	"fmt"
	"os"
)

type LoggerConsoleWriter struct {
	// The logging format
	format         string
	logLvl         LogLevel
	logLvlOp       LogOp // 0:= 1:>=, 2:<=
	curr_file_size int64
}

func NewLoggerConsoleWriter() *LoggerConsoleWriter {
	rval := &LoggerConsoleWriter{
		format:         "[%d %t] [%L] %M (%s)\r\n",
		curr_file_size: 0,
		logLvl:         LogLV_0,
		logLvlOp:       LogOp_ALL,
	}
	return rval
}

func (this *LoggerConsoleWriter) SetMsgFormat(fmt string) {
	this.format = fmt
}

func (this *LoggerConsoleWriter) LogWrite(rec *LogRecord) {
	if !IsFilter(this.logLvl, this.logLvlOp, rec) {
		return
	}
	msg := string(FormatLogRecord(this.format, rec))
	DefaultWorkers().PostTaskFunc(this, func() { // 不阻塞
		fmt.Fprint(os.Stdout, msg)
	})
}

func (this *LoggerConsoleWriter) Close() {

}

func (this *LoggerConsoleWriter) SetLvlFilter(lvl LogLevel, matchOp LogOp) *LoggerConsoleWriter {
	this.logLvl = lvl
	this.logLvlOp = matchOp
	return this
}
